var util = require('../../utils/util.js');
var app = getApp();
Page({
    data: {
        array: {},
        index:0,
        btns:{
            in:"primary",
            out:"default"
        },
        task: {
            name: '',
            address: '点击选择地点',
            qdaddress: '',
            qtaddress:'',
            province:'',
            city:'',
            project_name:'',
            state:'',
            outside:1,
            signTime: '00:00',
            signEarlyTime: '00:00',
            startDay: '2016-11-00',
            endDay: '2016-11-00',
            repeat: {
                'monday': 1,
                'tuesday': 1,
                'wednesday': 1,
                'thursday': 1,
                'friday': 1,
                'saturday': 0,
                'sunday': 0
            },
        },
        openId: '',
        userInfo: {},
        radioArray: [{ name: '内勤', value: 1, checked: 'true' },{ name: '外勤', value: 2 }],
        creating: false,
        button: {
            txt: '签到'
        },
        modalHidden: true
    },

    bindPickerChange: function (e) {
        this.setData({
            'index': e.detail.value,
            'task.project_name': e.detail.value
        });
    },

    bindPickerChange_hx: function (e) {
        this.setData({   //给变量赋值
            'index': e.detail.value,
            'task.project_num': app.globalData.projectArr[e.detail.value].ProjectNum,
            'task.project_name': app.globalData.projectArr[e.detail.value].ProjectName
        })
    },

    listenerRadioGroup: function (e) {
        this.setData({   //给变量赋值
            'task.outside': e.detail.value,
        })
    },

    // 设置任务名称
    bindKeyInput: function (e) {
        this.setData({
            'task.remarkvalue': e.detail.value
        });
    },
    // 设置任务地点
    chooseLocation: function () {
        var that = this;
        wx.chooseLocation({
            success: function(res){
                that.setData({
                    'task.address': res.address,
                    'task.latitude': res.latitude,
                    'task.longitude': res.longitude
                })
            },
            fail: function() {
                // fail
            },
            complete: function() {
                // complete
            }
        })
    },

    // 设置打卡时间
    setSignTime: function (e) {
        var that = this;
        var hour = ((+e.detail.value.slice(0, 2) + 24 - 2) % 24).toString();
        that.setData({
            'task.signTime': e.detail.value,
            'task.signEarlyTime': (hour[1] ? hour : '0' + hour) + ':' + e.detail.value.slice(3, 5)
        });
    },

    // 设置开始日期
    startDateChange: function (e) {
        this.setData({
            'task.startDay': e.detail.value
        })
    },

    // 设置结束日期
    endDateChange: function (e) {
        this.setData({
            'task.endDay': e.detail.value
        })
    },

    // 设置重复日
    changeMonday: function (e) {
        var state = this.data.task.repeat.monday;
        this.setData({
            'task.repeat.monday': (state == 1 ? 0 : 1)
        });
    },
    changeTuesday: function (e) {
        var state = this.data.task.repeat.tuesday;
        this.setData({
            'task.repeat.tuesday': (state == 1 ? 0 : 1)
        });
    },
    changeWednesday: function (e) {
        var state = this.data.task.repeat.wednesday;
        this.setData({
            'task.repeat.wednesday': (state == 1 ? 0 : 1)
        });
    },
    changeThursday: function (e) {
        var state = this.data.task.repeat.thursday;
        this.setData({
            'task.repeat.thursday': (state == 1 ? 0 : 1)
        });
    },
    changeFriday: function (e) {
        var state = this.data.task.repeat.friday;
        this.setData({
            'task.repeat.friday': (state == 1 ? 0 : 1)
        });
    },
    changeSaturday: function (e) {
        var state = this.data.task.repeat.saturday;
        this.setData({
            'task.repeat.saturday': (state == 1 ? 0 : 1)
        });
    },
    changeSunday: function (e) {
        var state = this.data.task.repeat.sunday;
        this.setData({
            'task.repeat.sunday': (state == 1 ? 0 : 1)
        });
    },

    // 隐藏提示弹层
    modalChange: function (e) {
        this.setData({
            modalHidden: true
        })
    },

    //

    // 创建任务
    createTask: function () {
        var that = this;
        var task = this.data.task;
        //var openId = this.data.openId;
        var openId = wx.getStorageSync('openId');
        var nickName = wx.getStorageSync('nickName');
        var avatarUrl = wx.getStorageSync('avatarUrl');
        var userInfo = this.data.userInfo;
        if (task.project_name===undefined){
            task.project_name = '';
        }
        if (task.project_num === undefined) {
            task.project_num = '';
        }
        if (task.remarkvalue === undefined) {
            task.remarkvalue = '';
        }
        var now = new Date();
        wx.request({
            url: 'https://xcx.ahdhst.cn/tp/public/index.php/Index/sign/createTask',
            data: {
                "key": app.globalData.key,
                name: task.name,
                address: task.address,
                province: task.province,
                city: task.city,
                state: task.state,
                startTime: task.startDay,
                endTime: task.endDay,
                signTime: task.signTime,
                latitude: task.latitude,
                longitude: task.longitude,
                monday: task.repeat.monday,
                tuesday: task.repeat.tuesday,
                wednesday: task.repeat.wednesday,
                thursday: task.repeat.thursday,
                friday: task.repeat.friday,
                saturday: task.repeat.saturday,
                sunday: task.repeat.sunday,
                openId: openId,
                nickName: nickName,
                avatarUrl: avatarUrl,
                remark:task.remarkvalue,
                outside:task.outside,
                project_num: task.project_num,
                project_name:task.project_name
            },
            method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            }, // 设置请求的 header
            success: function(res){
                if(res.data.data.msg!==undefined){
                    wx.showToast({
                        title: res.data.data.msg,
                        icon: 'loading',
                    });
                }
                var command = res.data.taskID;

                if (res.data.data.endTime !== undefined && res.data.data.endTime !== null) {
                    var endTimes = ' 签退时间 ' + res.data.data.endTime;
                    that.setData({
                        'button.txt': res.data.data.buttonTxt,
                        'qtsj': endTimes,
                        'qtsjvalue': res.data.data.endTime
                    });
                }
                if (res.data.data.startTime !== undefined && res.data.data.startTime !== null) {
                    var startTimes = ' 签到时间 ' + res.data.data.startTime;
                    that.setData({
                        'button.txt': res.data.data.buttonTxt,
                        'qdsj':startTimes,
                        'qdsjvalue':res.data.data.startTime
                    });
                }
                if (res.data.data.qdaddress !== undefined && res.data.data.qdaddress !== null) {
                    that.setData({
                        'task.qdaddress': res.data.data.qdaddress
                    });
                }
                if (res.data.data.qtaddress !== undefined && res.data.data.qtaddress !== null) {
                    that.setData({
                        'task.qtaddress': res.data.data.qtaddress
                    });
                }
            },
            fail: function() {
                // fail
            },
            complete: function() {
                // complete
            }
        })
    },

    // 提交、检验
    bindSubmit: function (e) {
        var that = this;
        var task = this.data.task;
        var creating = this.data.creating;

        if (task.name == '' || task.address == '点击选择地点' ) {
            this.setData({
                modalHidden: false
            });
        } else {
            if (!creating) {
                this.setData({
                    'creating': true
                });
                that.createTask();
            }
        }
    },

    onShow: function () {
        // 恢复新建按钮状态
        this.setData({
            'creating': false
        });
    },

    onHide: function () {

    },

    // 初始化设置
    onLoad: function () {
        var that = this;
        var now = new Date();
        var openId = wx.getStorageSync('openId');
        var userInfo = this.data.userInfo;
        this.refresh();
        this.setData({
            'task.name': 123
        });
        // 初始化打卡时间
        that.setData({
            'task.signTime': util.getHM(now),
            'task.nowDate': util.getYMD(now),
            'task.nowWeek': util.getW(now),
            'task.signEarlyTime': util.getHM(new Date(now.getTime() - 1000 * 3600 * 2)),
        });

        // 初始化日期
        that.setData({
            'task.startDay': util.getYMD(now),
            'task.endDay': util.getYMD(now)
        });

        app.getUserInfo(function (userInfo) {
            //更新数据
            that.setData({
                userInfo: userInfo
            });

            that.setData({
                openId: openId
            });
        });
        // 初始化昵称
    },
    refresh:function(){
        var that = this;
        var openId = wx.getStorageSync('openId');
        wx.getLocation({
            type: 'gcj02', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
            success: function (res) {
                var locationString = res.latitude + "," + res.longitude;   //获取的经度纬度
                that.setData({
                    'task.latitude': res.latitude,
                    'task.longitude': res.longitude,
                })
                console.log(locationString)
                wx.request({
                    url: 'https://xcx.ahdhst.cn/tp/public/index.php/Index/sign/getLocation',  //根据经纬度转换成具体地址
                    data: {
                        "key": app.globalData.key,
                        "location": locationString
                    },
                    method: 'POST',
                    header: {
                        "Content-Type": "application/x-www-form-urlencoded"
                    }, // 设置请求的 header
                    // header: {},
                    success: function (res) {
                        if (res.data.result.address!==undefined){
                            that.setData({
                                'task.address': res.data.result.address,
                                'task.province': res.data.result.address_component.province,
                                'task.city': res.data.result.address_component.city,
                                'task.state': res.data.result.address_component.district
                            })
                        }
                        if (res.data.result.company_name!==undefined){
                            that.setData({
                                'task.company_name': res.data.result.company_name
                            })
                        }
                        console.log("请求数据:" + res.data.result.address);
                    },
                    fail: function () {
                        // fail
                        console.log("请求失败");
                    },
                    complete: function () {
                        // complete
                        console.log("请求完成");
                    }
                });
                wx.request({
                    url: 'https://xcx.ahdhst.cn/tp/public/index.php/Index/sign/task',  //根据经纬度转换成具体地址
                    data: {
                        "key": app.globalData.key,
                        "openId": openId,
                    },
                    method: 'POST',
                    header: {
                        "Content-Type": "application/x-www-form-urlencoded"
                    },
                    success: function (res) {
                        that.setData({
                            'button.txt': '签到',
                        });
                        if (res.data.data.endTime !== undefined && res.data.data.endTime !== null) {
                            var endTimes = ' 签退时间 ' + res.data.data.endTime;
                            that.setData({
                                'button.txt': res.data.data.buttonTxt,
                                'qtsj':endTimes,
                                'qtsjvalue':res.data.data.endTime
                            });
                        }
                        if (res.data.data.startTime !== undefined && res.data.data.startTime !== null) {
                            var startTimes = ' 签到时间 ' + res.data.data.startTime;
                            that.setData({
                                'button.txt': res.data.data.buttonTxt,
                                'qdsj':startTimes,
                                'qdsjvalue': res.data.data.startTime
                            });
                        }
                        if (res.data.project !== undefined && res.data.project !== null) {
                            app.globalData.projectArr = res.data.project;
                            that.setData({
                                array: res.data.project
                            });
                        }
                        if (res.data.data.remark !== undefined && res.data.data.remark !== null) {
                            that.setData({
                                'task.remark': res.data.data.remark
                            });
                        }
                        if (res.data.data.qdaddress !== undefined && res.data.data.qdaddress !== null) {
                            that.setData({
                                'task.qdaddress': res.data.data.qdaddress
                            });
                        }
                        if (res.data.data.qtaddress !== undefined && res.data.data.qtaddress !== null) {
                            that.setData({
                                'task.qtaddress': res.data.data.qtaddress
                            });
                        }
                        that.setData({
                            creating: res.data.data.creating
                        });
                    },
                    fail: function () {
                        // fail
                        console.log("请求失败");
                    },
                    complete: function () {
                        // complete
                        console.log("请求完成");
                    }
                })
            },
            fail: function () {
                // fail
                console.log("定位失败");
            },
            complete: function () {
                // complete
                console.log("定位完成");
            }
        })
    },
    in_click:function(e){
        this.setData({
            btns:{
                in:"primary",
                out:"default"
            },
        });
        this.data.task.outside = 1;
        //this.data.radioArray[0]["checked"]=true;
        //this.data.radioArray[1]["checked"]=false;
    },
    out_click:function(e){
        this.setData({
            btns:{
                in:"default",
                out:"primary"
            },
        });
        this.data.task.outside = 2;
        //this.data.radioArray[1]["checked"]=true;
        //this.data.radioArray[0]["checked"]=false;
    },
    refresh_click:function(){
        this.refresh();
    }
})